package ImageFileSet
{
	/*
/uploadsfolder (mine is C:\DP\uploads )
    /25xxx (the directory we download the raw images into)
        /25xxx (you create this one, and the next one too)
            /25xxx-page-images (this contains your output files)
    /25xxy (same deal, next project)
        /25xxy
            /25xxy-page-images
	*/
	
    import ImageFile.ImageFile;
    
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    
    import mx.controls.Alert;
    import mx.collections.ArrayCollection;
    
    [Bindable]
	public class ImageFileSet extends ArrayCollection
	{
		private var _dir:File;
		private var _text:String = "";
		private var _pages:ArrayCollection;
		private var jsonRead:String;
		private var jsonWrite:String;
		
		public function ImageFileSet(dir:File)
		{
            _dir = dir;
            loadFilenames();
            if(this.length <= 0) {
                Alert.show("No image files found in " + this.path);
                return;
            }
            var ifa:Array = loadMap();
            if(ifa && ifa.length) {
                var o:Object;
                var j:ImageFile;
                for each(o in ifa) {
                    j = this.getItemByName(o.filename);
                    if(j != null) {
                        j.species = o.species;
                        j.remark = o.remark;
                        j.section = o.section;
                        j.pageNumber = o.pageNumber; 
                    }
                }
            }
		}
		
        public function get exists():Boolean {
            return (length > 0);
        }
        
//        public function get bytesTotal():Number {
//        	var isum:uint = 0;
//        	if(! exists) {
//        	    return 0;
//        	}
//        	else {
//        		for(var i:uint = 0; i < length; i++) {
//        			isum += this[i].size;
//        		}
//        		return isum;
//        	}
//        }
//        
        public function deleteMap():void {
        	if(mapFile.exists) {
            	mapFile.deleteFile();
            }
        }
        private function get mapFile():File {
        	var s:String = ImageFile.ImageFile.buildPath(_dir.nativePath, "imagemap");
        	return _dir.resolvePath(s);
        }
        
        public function saveMap():void {
            if(length === 0) {
            	if(mapFile.exists)
                    mapFile.deleteFile();
            	return;
            }
            var a:Array = new Array();
            var i:ImageFile;
            for each(i in this) {
                a[a.length] = i;
            }
            var s:FileStream = new FileStream();
            s.open(mapFile, FileMode.WRITE);
            try {
                s.writeObject(a);
            } catch(e:Error) {
                var str:String = e.message;
            }
            s.close()
//            jsonWrite = JSON.encode(this);
//            var fs:FileStream = new FileStream();
//            fs.open(mapFile, FileMode.WRITE);
//            fs.writeObject(jsonWrite);
//            fs.close();
        }
        
       public function loadMap():Array {
            if(! mapFile.exists) {
                return null;
            }
           var ary:Array;
            var s:FileStream = new FileStream();
            s.open(mapFile, FileMode.READ);
            try {
                ary = s.readObject();
            }
            catch(e:Error) {
                s.close();
                mapFile.deleteFile();
                return null;
            }
            s.close();
            return ary;
//                var stream:FileStream = new FileStream();
//                stream.open(mapFile, FileMode.READ);
//                jsonRead = stream.readObject() as String;
//                stream.close();
//                var j:* = JSON.decode(jsonRead);
//                for(var i:uint = 0; i < j.list.source.length; i++) {
//                	mapItem(j.list.source[i]);
//                }
        }
        
        private function getItemByName(str:String):ImageFile {
            var o:Object;
            for each(o in this) {
            	if(o.name == str) {
            		return ImageFile (o);
            	}
            }	
            return null;
        }
        
        private function mapItem(o:Object):void {
        	var f:ImageFile = this.getItemByName(o.name);
        	if(f) {
        		f.section = o.section;
        		f.pageNumber = o.pageNumber;
        		f.species = o.species;
        		f.remark = o.remark;
        	}
        }
        
        public function getItem(idx:uint):ImageFile {
        	return ImageFile (this.getItemAt(idx));
        }
        
        public function get imageFiles():ArrayCollection {
        	return this;
        }
        
        public function get path():String {
        	return _dir.nativePath;
        }
        
        public function clearSections():void {
        	for(var i:Number = 0; i < length; i++) {
                this.getItem(i).section = "";
            }
        }
        public function clearPageNumbers():void {
            for(var i:Number = 0; i < length; i++) {
                this.getItem(i).pageNumber = "";
            }
        }
        public function clearSpecies():void {
            for(var i:Number = 0; i < length; i++) {
                this.getItem(i).species = "";
            }
        }
        public function clearRemarks():void {
            for(var i:Number = 0; i < length; i++) {
                this.getItem(i).remark = "";
            }
        }
        private function loadFilenames():void {
	            var flist:Array = _dir.getDirectoryListing();
	        this.removeAll();
	
            for(var i:int = 0; i < flist.length; i++) {
               var f:ImageFile = new ImageFile(flist[i]);
               if(f.extension === "png" || f.extension === "jpg"
                   || f.extension === "bmp" || f.extension === "tiff"
                   || f.extension === "tif") {
                   addItem(f);
               }
            }
        }
        
        public function Clear():void {
            for(var i:uint=0; i < length; i++) {
                var r:ImageFile = getItem(i);
                r.clear();
            }
        }
        
        public function RenameFiles(andText:Boolean=false):void {
        	for(var i:uint=0; i < length; i++) {
        		var r:ImageFile = getItem(i);
        		r.copyRename(andText);
        	}
        }
        
        public static function buildPath(p1:String, p2:String):String {
            for( ; p1.length > 0 && ( p1.substr(-1) === "\\" || p1.substr(-1) === "/"); ) {
                p1 = p1.substr(0, p1.length-1);
            }
            for( ; p2.length > 0 && ( p2.substr(0, 1) === "\\" || p2.substr(0, 1) === "/"); ) {
                p2 = p2.substr(1);
            }
            return p1 + "/" + p2;
        }
        
//        public function get text():String {
//            if(! _text) {
//                _pages = new ArrayCollection();
//                if(this.length <= 0) {
//                    return "";
//                }
//                for each(var i:ImageFile in this) {
//                    i.offset = _text.length;
//                    _pages.addItemAt(i, i.offset);
//                    _text += i.text;    
//                }
//            }
//            return _text;
//        }
    }
}    

